<?php
define("INC",1);
class IndexAction extends CommonAction {
    function index(){
        $mem=M('members');
        $uid=session("uid");
        $meminfo=$mem->find($uid);
        
        //统计外链发布情况
        $links=M('links');
        $todaytimestamp=strtotime(date("Y-m-d",  time()));//今天0点的时间戳
        $yestodaytimestamp=strtotime(date("Y-m-d",  time()))-86400;//昨天的时间戳
        $weektimestamp=strtotime(date("Y-m-d",  time()))-86400*6;//6天前的时间戳
        $monthtimestamp=strtotime(date("Y-m-d",  time()))-86400*30;//6天前的时间戳
        //当天
        $where=array(
            "uid"=>$uid,
            "pubdate"=>array("gt",$todaytimestamp)
        );
        $todaypub=array();
        $todaypub['total']=$links->field("linkid")->where($where)->count();//外链总数
        $where['bdstatus']=1;
        $todaypub['bdgood']=$links->field("linkid")->where($where)->count();//收录有效
        unset($where['bdstatus']);
        $where['pubstatus']=1;
        $todaypub['pubgood']=$links->field("linkid")->where($where)->count();//发布有效
        unset($where['pubstatus']);
        $where['pubstatus']=1;
        $where['bdstatus']=1;
        $todaypub['allgood']=$links->field("linkid")->where($where)->count();//有效外链
        //昨天
        $where=array(
            "uid"=>$uid,
            "pubdate"=>array("between",array($yestodaytimestamp,$todaytimestamp))
        );
        $yesdaypub=array();
        $yesdaypub['total']=$links->field("linkid")->where($where)->count();//外链总数
        $where['bdstatus']=1;
        $yesdaypub['bdgood']=$links->field("linkid")->where($where)->count();//收录有效
        unset($where['bdstatus']);
        $where['pubstatus']=1;
        $yesdaypub['pubgood']=$links->field("linkid")->where($where)->count();//发布有效
        unset($where['pubstatus']);
        $where['pubstatus']=1;
        $where['bdstatus']=1;
        $yesdaypub['allgood']=$links->field("linkid")->where($where)->count();//有效外链
        
        //一周内
        $where=array(
            "uid"=>$uid,
            "pubdate"=>array("gt",$weektimestamp)
        );
        $weekpub=array();
        $weekpub['total']=$links->field("linkid")->where($where)->count();//外链总数
        $where['bdstatus']=1;
        $weekpub['bdgood']=$links->field("linkid")->where($where)->count();//收录有效
        unset($where['bdstatus']);
        $where['pubstatus']=1;
        $weekpub['pubgood']=$links->field("linkid")->where($where)->count();//发布有效
        unset($where['pubstatus']);
        $where['pubstatus']=1;
        $where['bdstatus']=1;
        $weekpub['allgood']=$links->field("linkid")->where($where)->count();//有效外链
        
        //一个月内
        $where=array(
            "uid"=>$uid,
            "pubdate"=>array("gt",$monthtimestamp)
        );
        $monthpub=array();
        $monthpub['total']=$links->field("linkid")->where($where)->count();//外链总数
        $where['bdstatus']=1;
        $monthpub['bdgood']=$links->field("linkid")->where($where)->count();//收录有效
        unset($where['bdstatus']);
        $where['pubstatus']=1;
        $monthpub['pubgood']=$links->field("linkid")->where($where)->count();//发布有效
        unset($where['pubstatus']);
        $where['pubstatus']=1;
        $where['bdstatus']=1;
        $monthpub['allgood']=$links->field("linkid")->where($where)->count();//有效外链
        
        //总数
        $where=array(
            "uid"=>$uid,
        );
        $totalpub=array();
        $totalpub['total']=$links->field("linkid")->where($where)->count();//外链总数
        $where['bdstatus']=1;
        $totalpub['bdgood']=$links->field("linkid")->where($where)->count();//收录有效
        unset($where['bdstatus']);
        $where['pubstatus']=1;
        $totalpub['pubgood']=$links->field("linkid")->where($where)->count();//发布有效
        unset($where['pubstatus']);
        $where['pubstatus']=1;
        $where['bdstatus']=1;
        $totalpub['allgood']=$links->field("linkid")->where($where)->count();//有效外链
        
        $this->assign("totalpub",$totalpub);
        $this->assign("monthpub",$monthpub);
        $this->assign("weekpub",$weekpub);
        $this->assign("yesdaypub",$yesdaypub);
        $this->assign("todaypub",$todaypub);
        $this->assign("meminfo",$meminfo);
        $this->display();
    }
    
   


    function useredit(){
        if(!$this->isPost()){
            $this->display();
        }else{
            $uid=session("uid");
            if(!$uid){
                exit('修改失败,重新登陆后再试');
            }
            $newpw=$this->_param("newpw");
            $newpw1=$this->_param("newpw1");
            if(strlen($newpw)<6){
                exit('密码不能少于6位');
            }
            if($newpw!=$newpw1){
                exit('两次密码不一致,请返回修改');
            }
            $passwd=md5($newpw);
            $mem=M('members');
            $mem->where(array("uid"=>$uid))->save(array("passwd"=>$passwd));
            exit('修改成功，重新登陆时生效');
        }
    }
    function test(){
        $ch1 = curl_init();
        $ch2 = curl_init();
        curl_setopt($ch1,CURLOPT_URL,"http://www.163.com"); //外链地址句柄
        curl_setopt($ch2,CURLOPT_URL, "http://www.baidu.com/s?wd=111");
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch1, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch2, CURLOPT_TIMEOUT, 10);
        $mh = curl_multi_init();
        curl_multi_add_handle($mh, $ch1);
        curl_multi_add_handle($mh, $ch2);
        $active = NULL;
        // 执行批处理句柄
        do {
            $mrc = curl_multi_exec($mh, $active);
            echo $mrc;
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        
/*
        while ($active && $mrc == CURLM_OK) {
            if (curl_multi_select($mh) != -1) {
                do {
                    $mrc = curl_multi_exec($mh, $active);
                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
            }
        }
 */
        echo curl_error($ch1);
        echo curl_error($ch2);
        $html1 = curl_multi_getcontent($ch1); //外链网址源码
        $html2 = curl_multi_getcontent($ch2); //百度收录检测源码
        // 关闭全部句柄
        curl_multi_remove_handle($mh, $ch1);
        curl_multi_remove_handle($mh, $ch2);
        curl_multi_close($mh);
        
        echo $html1;
        echo $html2;
    }
}
